<?php

	fta_registerModules(
		array(
			"FTAORM/FTAORM",
		)
	);
	
class FTAORMCMSManyToManyWrapper extends FTAORM  {
	const TABLE_SEQUENCE_NAME = 'manytomanydatatablesequence_seq';
	function __construct($zdb) {
		
		parent::__construct($zdb);			
			$this->primaryKey = "manytomanycode";
			$this->tableFields = array(
				'parentcode',
				'relatedcode',
				'elementid'	
			);//excluding the pk..
									
			$this->tableFieldTypes = array(
				'manytomanycode' => 'number' ,
				'parentcode' => 'number',
				'relatedcode' => 'number',
				'elementid'	=>'number'								
			);
						
			// $this->tableRequiredFields = array("title", "first_name", "last_name", "email", "phone", "address", "city", "country_id");
			$this->tableRequiredFields = array(
				'parentcode',
				'relatedcode',
				'elementid'	
			
			);
			
			$this->tableName = "manytomanydata";	
			
			$this->tableRelationships = array();

			$this->tableRelationshipClasses = array();	
	
		}	

		static function getAllManyToManyRecordsForParentCodeAndElementId($parentCode, $elementId, $zdb){
			$myClass = get_called_class();
			
			$m = new $myClass($zdb);
			
			$zdb->setFetchMode(\Zend_Db::FETCH_ASSOC);
			
			$select = $zdb->select()// we may want to simplify this.. these should be a bit smarter..
            	->from(array('m' => $m->tableName))
                ->where('parentcode = ?', $parentCode)
            	->where('elementid = ?', $elementId);
            	
			$statement = $select->query();
			$result = $statement->fetchAll();
		
            return static::rowsToObjects($result,$myClass,$zdb);			
		}
		
		static function getAllManyToManyRecordsForParentCode($parentCode, $zdb){
			$myClass = get_called_class();
			
			$m = new $myClass($zdb);
			
			$zdb->setFetchMode(\Zend_Db::FETCH_ASSOC);
			
			$select = $zdb->select()// we may want to simplify this.. these should be a bit smarter..
            	->from(array('m' => $m->tableName))
                ->where('parentcode = ?', $parentCode);
            	
			$statement = $select->query();
			$result = $statement->fetchAll();
		
            return static::rowsToObjects($result,$myClass,$zdb);			
		}		
		
		
		static function getNewPrimaryKey(Zend_Db_Adapter_Abstract $zdb){			
			$zdb->query('UPDATE ' . static::$TABLE_SEQUENCE_NAME .' SET id=LAST_INSERT_ID(id+1)');
			$id = $zdb->lastInsertId();
			return $id;
		}	
	
	
	
	
}